package org.opens.converter;

import org.opens.converter.mybatisJdbcType.I_MybatisJdbcTypeConverter;
import org.opens.converter.mybatisJdbcType.MybatisJdbcTypeConverterEntity;
import org.opens.converter.mybatisJdbcType.MybatisJdbcTypeConverterFactory;
import org.opens.converter.sqlType.I_SqlTypeConverter;
import org.opens.converter.sqlType.SqlTypeConverterEntity;
import org.opens.converter.sqlType.SqlTypeConverterFactory;
import org.opens.db.constant.DbType;

/**
 * 所有转换器的工厂入口
 *
 * @author by zhangyuming
 * @date 2023/3/7
 */
public class ConverterFactory {

    /**
     * 根据key获取工厂实例; 这个方法总是期望获取到合适的工厂, 否则将会抛出异常
     * @param key   工厂对应的key
     * @return  key对应的工厂
     */
    public static AbstractConverterFactory getInstance(ConverterFactoryKey key) {
        if (ConverterFactoryKey.SQL_TYPE_CONVERTER_FACTORY == key) {
            return new SqlTypeConverterFactory();
        } else if (ConverterFactoryKey.MYBATIS_JDBC_TYPE_CONVERTER_FACTORY == key) {
            return new MybatisJdbcTypeConverterFactory();
        }

        throw new RuntimeException("未找到合适的工厂!");
    }

    public static void main(String[] args) {
        AbstractConverterFactory sqlTypeFactory = getInstance(ConverterFactoryKey.SQL_TYPE_CONVERTER_FACTORY);
        I_SqlTypeConverter sqlTypeConverter = sqlTypeFactory.getSqlTypeConverter(DbType.MYSQL);
        SqlTypeConverterEntity res1 = sqlTypeConverter.dbType2JavaType("int");
        System.out.println(res1);

        AbstractConverterFactory mybatisJdbcTypeFactory = getInstance(ConverterFactoryKey.MYBATIS_JDBC_TYPE_CONVERTER_FACTORY);
        I_MybatisJdbcTypeConverter mybatisJdbcTypeConverter = mybatisJdbcTypeFactory.getMybatisJdbcTypeConverter(DbType.MYSQL);
        MybatisJdbcTypeConverterEntity res2 = mybatisJdbcTypeConverter.dbType2JdbcType("int");
        System.out.println(res2);
    }

}
